<template><div><h2 id="使用表单" tabindex="-1"><a class="header-anchor" href="#使用表单"><span>使用表单</span></a></h2>
<p>本章节介绍如何创建一个让用户提交数据的表单页。该页将显示一个包含 name 输入框和 email 输入框的表单。当提交这两部分信息后，页面将会显示用户所输入的信息。</p>
<p>为了实现这个目标，除了创建一个 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/structure-controllers.md" target="_blank" rel="noopener noreferrer">操作</a> 和两个 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/structure-views" target="_blank" rel="noopener noreferrer">视图</a> 外，还需要创建一个 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/structure-models.md" target="_blank" rel="noopener noreferrer">模型</a> 。</p>
<p>通过本教程，你将会学到：</p>
<ul>
<li>创建一个 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/structure-models.md" target="_blank" rel="noopener noreferrer">模型</a> 代表用户通过表单输入的数据</li>
<li>声明规则去验证输入的数据</li>
<li>在 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/structure-views.md" target="_blank" rel="noopener noreferrer">视图</a> 中生成一个 HTML 表单</li>
</ul>
<h2 id="创建模型" tabindex="-1"><a class="header-anchor" href="#创建模型"><span>创建模型</span></a></h2>
<p>模型类 <code v-pre>EntryForm</code> 代表从用户那请求的数据，该类如下所示并存储在 <code v-pre>models/EntryForm.php</code> 文件中。请参考 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/concept-autoloading.md" target="_blank" rel="noopener noreferrer">类自动加载</a> 章节获取更多关于类命名约定的介绍。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">EntryForm</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$name</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$email</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">rules</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该类继承自Yii 提供的一个基类 <code v-pre>yii\base\Model</code>，该基类通常用来表示数据。</p>
<blockquote>
<p>Info: <code v-pre>yii\base\Model</code> 被用于普通模型类的父类并与数据表<strong>无关</strong>。<code v-pre>yii\db\ActiveRecord</code> 通常是普通模型类的父类但与数据表有关联（译注：<code v-pre>yii\db\ActiveRecord</code> 类其实也是继承自 <code v-pre>yii\base\Model</code>，增加了数据库处理）。</p>
</blockquote>
<p><code v-pre>EntryForm</code> 类包含 <code v-pre>name</code> 和 <code v-pre>email</code> 两个公共成员，用来储存用户输入的数据。它还包含一个名为 <code v-pre>rules()</code> 的方法，用来返回数据验证规则的集合。上面声明的验证规则表示：</p>
<ul>
<li><code v-pre>name</code> 和 <code v-pre>email</code> 值都是必须的</li>
<li><code v-pre>email</code> 的值必须满足email规则验证</li>
</ul>
<p>如果你有一个处理用户提交数据的 <code v-pre>EntryForm</code> 对象，你可以调用它的 [[yii\base\Model::validate()|validate()]] 方法触发数据验证。<br>
如果有数据验证失败，将把 [[yii\base\Model::hasErrors|hasErrors]] 属性设为 ture，想要知道具体发生什么错误就调用 [[yii\base\Model::getErrors|getErrors]]。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token variable">$model</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EntryForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$model</span><span class="token operator">-></span><span class="token property">name</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Qiang'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$model</span><span class="token operator">-></span><span class="token property">email</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'bad'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$model</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 验证成功！</span></span>
<span class="line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 失败！</span></span>
<span class="line">    <span class="token comment">// 使用 $model->getErrors() 获取错误详情</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="创建动作" tabindex="-1"><a class="header-anchor" href="#创建动作"><span>创建动作</span></a></h2>
<p>下面你得在 <code v-pre>site</code> 控制器中创建一个 <code v-pre>entry</code> 操作用于新建的模型。动作的创建和使用已经在 <a href="https://learnku.com/docs/yii-framework/2.0.x/hello-world/11978" target="_blank" rel="noopener noreferrer">Hello world</a> 小节中解释了。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">app<span class="token punctuation">\</span>models<span class="token punctuation">\</span>EntryForm</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SiteController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...现存的代码...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">actionEntry</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$model</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EntryForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$model</span><span class="token operator">-></span><span class="token function">load</span><span class="token punctuation">(</span><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">request</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token variable">$model</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 验证 $model 收到的数据</span></span>
<span class="line"></span>
<span class="line">            <span class="token comment">// 做些有意义的事 ...</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">render</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'entry-confirm'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'model'</span> <span class="token operator">=></span> <span class="token variable">$model</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 无论是初始化显示还是数据验证错误</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">render</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'entry'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'model'</span> <span class="token operator">=></span> <span class="token variable">$model</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该操作首先创建了一个 <code v-pre>EntryForm</code> 对象。然后尝试从 <code v-pre>$_POST</code> 搜集用户提交的数据，由 Yii 的 [[yii\web\Request::post()]] 方法负责搜集。如果模型被成功填充数据（也就是说用户已经提交了 HTML 表单），操作将调用 [[yii\base\Model::validate()|validate()]] 去确保用户提交的是有效数据。</p>
<blockquote>
<p>Info: 表达式 <code v-pre>Yii::$app</code> 代表 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/structure-applications.md" target="_blank" rel="noopener noreferrer">应用</a> 实例，它是一个全局可访问的单例。同时它也是一个<a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/concept-service-locator.md" target="_blank" rel="noopener noreferrer">服务定位器</a>，能提供 <code v-pre>request</code>，<code v-pre>response</code>，<code v-pre>db</code> 等等特定功能的组件。在上面的代码里就是使用 <code v-pre>request</code> 组件来访问应用实例收到的 <code v-pre>$_POST</code> 数据。</p>
</blockquote>
<p>用户提交表单后，操作将会渲染一个名为 <code v-pre>entry-confirm</code> 的视图去确认用户输入的数据。如果没填表单就提交，或数据包含错误（译者：如 email 格式不对），<code v-pre>entry</code> 视图将会渲染输出，连同表单一起输出的还有验证错误的详细信息。</p>
<blockquote>
<p>Note: 在这个简单例子里我们只是呈现了有效数据的确认页面。实践中你应该考虑使用 [[yii\web\Controller::refresh()|refresh()]] 或 [[yii\web\Controller::redirect()|redirect()]] 去避免 <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get" target="_blank" rel="noopener noreferrer">表单重复提交问题</a> 。</p>
</blockquote>
<h2 id="创建视图" tabindex="-1"><a class="header-anchor" href="#创建视图"><span>创建视图</span></a></h2>
<p>最后创建两个视图文件 <code v-pre>entry-confirm</code> 和 <code v-pre>entry</code>。他们会被刚才创建的 <code v-pre>entry</code> 操作渲染。</p>
<p><code v-pre>entry-confirm</code> 视图简单地显示提交的 name 和 email 数据。视图文件应该保存在 <code v-pre>views/site/entry-confirm.php</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Html</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>You have entered the following information:<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span></span>
<span class="line"></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>label</span><span class="token punctuation">></span></span>Name<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>label</span><span class="token punctuation">></span></span>: <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token class-name static-context">Html</span><span class="token operator">::</span><span class="token function">encode</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>label</span><span class="token punctuation">></span></span>Email<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>label</span><span class="token punctuation">></span></span>: <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token class-name static-context">Html</span><span class="token operator">::</span><span class="token function">encode</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>entry</code> 视图显示一个 HTML 表单。视图文件应该保存在 <code v-pre>views/site/entry.php</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Html</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>widgets<span class="token punctuation">\</span>ActiveForm</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token variable">$form</span> <span class="token operator">=</span> <span class="token class-name static-context">ActiveForm</span><span class="token operator">::</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$form</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$form</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>form-group<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token class-name static-context">Html</span><span class="token operator">::</span><span class="token function">submitButton</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Submit'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'btn btn-primary'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token class-name static-context">ActiveForm</span><span class="token operator">::</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>视图使用了一个功能强大的 <a href="https://learnku.com/docs/yii-framework/2.0.x/use-form/structure-widgets.md" target="_blank" rel="noopener noreferrer">小部件</a> [[yii\widgets\ActiveForm|ActiveForm]] 去生成 HTML 表单。其中的 <code v-pre>begin()</code> 和 <code v-pre>end()</code> 分别用来渲染表单的开始和关闭标签。在这两个方法之间使用了 [[yii\widgets\ActiveForm::field()|field()]] 方法去创建输入框。第一个输入框用于 “name”，第二个输入框用于 “email”。之后使用 [[yii\helpers\Html::submitButton()]] 方法生成提交按钮。</p>
<h2 id="尝试下" tabindex="-1"><a class="header-anchor" href="#尝试下"><span>尝试下</span></a></h2>
<p>用浏览器访问下面的 URL 看它能否工作：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">http://hostname/index.php?r<span class="token operator">=</span>site/entry</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你会看到一个包含两个输入框的表单的页面。每个输入框的前面都有一个标签指明应该输入的数据类型。如果什么都不填就点击提交按钮，或填入格式不正确的 email 地址，将会看到在对应的输入框下显示错误信息。</p>
<p><img src="https://cdn.learnku.com/uploads/images/202201/26/93088/23LtBnVV4H.png!large" alt="验证错误的表单"></p>
<p>输入有效的 name 和 email 信息并提交后，将会看到一个显示你所提交数据的确认页面。</p>
<p><img src="https://cdn.learnku.com/uploads/images/202201/26/93088/xdHfRwbxPP.png!large" alt="输入数据的确认页"></p>
<h3 id="效果说明" tabindex="-1"><a class="header-anchor" href="#效果说明"><span>效果说明</span></a></h3>
<p>你可能会好奇 HTML 表单暗地里是如何工作的呢，看起来它可以为每个输入框显示文字标签，而当你没输入正确的信息时又不需要刷新页面就能给出错误提示，似乎有些神奇。</p>
<p>是的，其实数据首先由客户端 JavaScript 脚本验证，然后才会提交给服务器通过 PHP 验证。<br>
[[yii\widgets\ActiveForm]] 足够智能到把你在 <code v-pre>EntryForm</code> 模型中声明的验证规则转化成客户端 JavaScript 脚本去执行验证。<br>
如果用户浏览器禁用了 JavaScript，服务器端仍然会像 <code v-pre>actionEntry()</code> 方法里这样验证一遍数据。这保证了任何情况下用户提交的数据都是有效的。</p>
<blockquote>
<p>Warning: 客户端验证是提高用户体验的手段。无论它是否正常启用，服务端验证则都是必须的，请不要忽略它。</p>
</blockquote>
<p>输入框的文字标签是 <code v-pre>field()</code> 方法生成的，内容就是模型中该数据的属性名。例如模型中的 <code v-pre>name</code> 属性生成的标签就是 <code v-pre>Name</code>。</p>
<p>你可以在视图中自定义标签，按如下方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$form</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">label</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'自定义 Name'</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$form</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">label</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'自定义 Email'</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Info: Yii 提供了相当多类似的小部件去帮你生成复杂且动态的视图。在后面你还会了解到自己写小部件是多么简单。你可能会把自己的很多视图代码转化成小部件以提高重用，加快开发效率。</p>
</blockquote>
<h2 id="总结" tabindex="-1"><a class="header-anchor" href="#总结"><span>总结</span></a></h2>
<p>本章节指南中你接触了 MVC 设计模式的每个部分。学到了如何创建一个模型代表用户数据并验证它的有效性。</p>
<p>你还学到了如何从用户那获取数据并在浏览器上回显给用户。这本来是开发应用的过程中比较耗时的任务，好在 Yii 提供了强大的小部件让它变得如此简单。</p>
<p>在下一章节中，你将学习如何使用数据库，几乎每个应用都需要数据库。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


